
Vue.component2('a_1_0_4_3_1', {  
    template: `
<div style="width:100%;height:100%" v-if="data.show_flag" :key="refresh">

  <div class="a_1_0_4_3_1_title">
    <div style="color:#bcbcbc">连线属性编辑框</div>
  </div>

  <div class="a_1_0_4_3_1_container">
    <div class="a_1_0_4_3_1_left">出参id
    </div>
    <div class="a_1_0_4_3_1_right">{{data.out_id}}
    </div>
  </div>

  <div class="a_1_0_4_3_1_container">
    <div class="a_1_0_4_3_1_left">入参id</div>
    <div class="a_1_0_4_3_1_right">{{data.in_id}}</div>
  </div>
  <div class="a_1_0_4_3_1_container">
    <div class="a_1_0_4_3_1_left">出参名称</div>
    <div class="a_1_0_4_3_1_right">{{data.out_name}}</div>
  </div>
  <div class="a_1_0_4_3_1_container">
    <div class="a_1_0_4_3_1_left">入参名称</div>
    <div class="a_1_0_4_3_1_right">{{data.in_name}}</div>
  </div>

  <div  class="a_1_0_4_3_1_container" @click="add"><div style="font-size:30px">+</div></div>
  <div v-for="(item,indexr) in data.put_data">
    <div class="a_1_0_4_3_1_container">
      <div class="a_1_0_4_3_1_left">{{item[0]}}</div>
      <div class="a_1_0_4_3_1_center">&gt;</div>
      <div class="a_1_0_4_3_1_right">{{item[1]}}</div>

    </div>
  </div>


</div>
`,  
  data(){  
    return {  
      refresh:'1',
      data:{
        old_out_md5:'',
        old_in_md5:'',

        now_select:'1',//当前选择的
        key:'',
        flow_data:{},
        show_flag:false,
        out_id:'1',
        in_id:'1',
        put_data:[],
        out_name:"333",
        in_name:"333",

      }
    }    

},
watch:{
  'data.key':function(e){
    this.data.out_id=e.split("=>")[0].split("_")[1];
    this.data.in_id=e.split("=>")[1].split("_")[1];
    try{
      console.log("start refresh 1")
      this.data.old_out_md5=md5(JSON.stringify(this.data.flow_data.dict[this.data.now_select].flow_dict[this.data.out_id]));
      this.data.old_in_md5=md5(JSON.stringify(this.data.flow_data.dict[this.data.now_select].flow_dict[this.data.in_id]));
      console.log("start refresh 2")
      this.init();
    }catch{}
  },
'data.show_flag':function(e){
// if(e){

// }
},
  'data.flow_data': {
    handler(newVal, oldVal) {
      if(!this.data.show_flag){return}
      let new_out_md5=null;
      let new_in_md5=null;
      try{
        new_out_md5=md5(JSON.stringify(this.data.flow_data.dict[this.data.now_select].flow_dict[this.data.out_id]));
        new_in_md5=md5(JSON.stringify(this.data.flow_data.dict[this.data.now_select].flow_dict[this.data.in_id]));
        this.flow_dict=this.data.flow_data.dict[this.data.now_select].flow_dict;
      }catch{
        this.data.show_flag=false;
        this.refresh_func();
        return
      }
      if(new_out_md5!=this.data.old_out_md5 || new_in_md5!=this.data.old_in_md5){
        this.init();
      }
      // console.log('User object changed:');
      // console.log('Old Value:', oldVal);
      // console.log('New Value:', newVal);
    },
    deep: true,
  }
},
methods:{
  
  check_line(out_id,in_id){
      
    let start_obj=this.flow_dict[in_id]
    let start_obj_in_kwargs=start_obj.in_kwargs;
    for (let kk in start_obj_in_kwargs){
      if (start_obj_in_kwargs[kk].in_params && start_obj_in_kwargs[kk].in_params[0]==out_id){
        return {"type":true,'data':start_obj_in_kwargs[kk].in_params[2]}
      }
    }
    return {"type":false}
  },//在已有连线的组件上进行连线
  add_line_params(out_id,in_id,check_line_res,out_params_info){
let _this=this;
    let key1=out_params_info.key1//出参组件的 出参 id
    let key2=out_params_info.key2//入参组件的 入参 id
    //1.更新 入参组件的 入参连线index计数
    //2.更新 入参组件的 入参连线 的数据
    let in_kwargs_data={}
    in_kwargs_data[key2]={"in_params":{},"in_type1":"1",'v1_':[out_id,key1]}
                    //--------输出的key和value
    in_kwargs_data[key2]['in_params']=[out_id,key1,check_line_res.data]
    //4.更新 出参组件的 出参连线index计数
    //5.更新 出参组件的 出参连线 的数据
    let out_kwargs_data={}
    out_kwargs_data[key1]={"out_params":{}}
                    //--------输出的key和value
    out_kwargs_data[key1]['out_params'][in_id]={}
    out_kwargs_data[key1]['out_params'][in_id][key2]={"key":check_line_res.data}
//更新数据 
let update_val={
"type":"change_data",
"data_type":"set_list",//类型 修改数据
update_params:[
  {
    name:"检查目标元素是否存在 存在则不执行该操作",
    path:`flow_chat_config.dict.1.dict.${this.data.now_select}.flow_dict.${in_id}.in_kwargs.${key2}.in_params`,
    message:"已经拥有此传参了",
    use:"check_true"
  }
],
"postput":[
  {
    'path':`flow_chat_config.dict.1.dict.${this.data.now_select}.flow_dict.${out_id}.out_kwargs`,
    'data':out_kwargs_data
  },
  //更新
  {
    'path':`flow_chat_config.dict.1.dict.${this.data.now_select}.flow_dict.${in_id}.in_kwargs`,
    'data':in_kwargs_data
  },
],
}
_this.data.全局配置对象.更新(update_val);
  },
  add_func:function(out_params_info){
    let _this=this;
    let has_line=_this.check_line(this.data.out_id,this.data.in_id);
    //如果拥有连线 那么添加参数
    if (has_line.type==true){
      _this.add_line_params(this.data.out_id,this.data.in_id,has_line,out_params_info)
  }},
  refresh_func:function(){
    this.refresh=this.refresh==='2'?'1':'2';
  },
  init:function(){
    let return_flag=false;
    if(!this.data.flow_data.dict[this.data.now_select] || !this.data.flow_data.dict[this.data.now_select]){return_flag=true}
    this.flow_dict=this.data.flow_data.dict[this.data.now_select].flow_dict;
    console.log("start_init:::::")
    //获取组件名称
    let out_dct=this.flow_dict[this.data.out_id];
    let in_dct=this.flow_dict[this.data.in_id];
    if(!out_dct || !in_dct){return_flag=true}
    if(return_flag){
      this.data.show_flag=false;
      this.refresh_func();
      return
    }

    this.data.out_name=out_dct.name;
    this.data.in_name=in_dct.name;
    //获取组件传递的参数关系
    let temp_put_data=[];
    for(let k in out_dct.out_kwargs){
      let out_params=out_dct.out_kwargs[k].out_params;
      if(out_params && out_params[this.data.in_id]){
        let temp_d=out_params[this.data.in_id];
        for(let k2 in temp_d){
          temp_put_data.push(
            [out_dct.out_kwargs[k].name,in_dct.in_kwargs[k2].name]
          )
        }
      }
    }
    
    this.data.put_data=temp_put_data;
    if(!this.data.put_data.length){
      this.data.show_flag=false;
    }
    // console.log("start_init::eeeee")
    this.refresh_func();
  },
  add:function(){
    let _this=this;
    let start_out_kwargs={};
      let end_in_kwargs={};
      // console.log("?1")
      let in_kwargs=this.flow_dict[this.data.in_id].in_kwargs;
      let out_kwargs=this.flow_dict[this.data.out_id].out_kwargs;
      // console.log("?2")
      if(!out_kwargs){Vue.prototype.$notify({type: 'error',message: '输出组件没有出参'});return}
      if(!in_kwargs){Vue.prototype.$notify({type: 'error',message: '输入组件没有入参'});return}
      
    // console.log("in_kwargs",in_kwargs)
      for (let key in in_kwargs){
        if (!in_kwargs[key].in_params){end_in_kwargs[key]=in_kwargs[key].name;}
      }

      if(!(Object.keys(end_in_kwargs)).length){Vue.prototype.$notify({type: 'error',message: '输入组件入参不需要传递了'});return}
      for (let key in out_kwargs){
        start_out_kwargs[key]=out_kwargs[key].name;
      }
    select2__({
      "title":`${ this.data.out_name}->${this.data.in_name}`,
    "select1":start_out_kwargs,"select2":end_in_kwargs,
      callback:function(out_params_info){_this.add_func(out_params_info)}
    })
  }
},
mounted:function(){
}
}
);

